std::format
takes as an argument a format string that contains replacement fields (surrounded with {}
) and a set of extra
arguments that will be formatted inside the replacement fields. Even if the format string is checked at compile-time, it is possible to have a
mismatch between the format string and the arguments. For example, when:
- The format string contains fewer replacement fields than the number of extra arguments:
std::format("{} {}", 1, 2, 3);
- The format string uses indexes for the replacement fields, but one index is missing:
std::format("{0} {0} {2}", 1, 2, 3);
In these cases, the extra arguments are silently ignored. In the best-case scenario, it leads to dead code. Otherwise, it is a typo, and the output
will not be intended.
This rule detects when the arguments of std::format
are unused in a way that doesn’t trigger S6487. Therefore, you should
make sure that S6487 is enabled with this rule.
Noncompliant code example
std::cout << std::format("{} {}", 1, 2, 3); // Noncompliant
std::cout << std::format("{0} {2}", 1, 2, 3); // Noncompliant
Compliant solution
std::cout << std::format("{} {} {}", 1, 2, 3); // Compliant
std::cout << std::format("{0} {1} {2} {1}", 1, 2, 3); // Compliant